# -*- coding: utf-8 -*-
"""
Created on Wed May  9 13:52:59 2018

@author: 朱震东
"""
import os,shutil
import skimage
from PIL import Image  #对应pillow包
import scipy.misc
from matplotlib import pyplot as plt
import numpy as np
from pylab import *
import random

with open('datasets/result.txt') as file_object:
    contents = file_object.read()
contents = contents.split('\n')
for i in range(len(contents)):
    if contents[i] == '':
        del contents[i]
    else:
        contents[i] = contents[i].split(' ')
print(contents)
print(len(contents))

def mycopyfile(srcfile,dstfile):
    if not os.path.isfile(srcfile):
        print("%s not exist!"%(srcfile))
    else:
        fpath,fname=os.path.split(dstfile)    #分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)                #创建路径
        shutil.copyfile(srcfile,dstfile)      #复制文件
        print("copy %s -> %s"%( srcfile,dstfile))

srcpath = 'datasets/test/'
detpath = 'data/train/'
detpath2 = 'data/val/'
for i in range(len(contents)):
    if i%6 == 6:
        mycopyfile(srcpath+contents[i][0],detpath2+contents[i][1]+'/'+contents[i][0])
    else:
        mycopyfile(srcpath+contents[i][0],detpath+contents[i][1]+'/'+contents[i][0])
        im = array(Image.open(srcpath+contents[i][0]))
        imn = skimage.util.random_noise(im, mode='gaussian', seed=None, clip=True)
        scipy.misc.imsave(detpath+contents[i][1]+'/noise'+contents[i][0], imn)
        rand = random.randint(0,99)
        if(rand < 50):
            a = random.randint(30,100)
            b = random.randint(30,100)
            c = random.randint(30,100)
            for ii in range(im.shape[0]):
                for j in range(im.shape[1]):
                    im[ii][j][0] = im[ii][j][0] - a
                    if im[ii][j][0] < 0:
                        im[ii][j][0] = 0
                    im[ii][j][1] = im[ii][j][1] - b
                    if im[ii][j][1] < 0:
                        im[ii][j][1] = 0
                    im[ii][j][2] = im[ii][j][2] - c
                    if im[ii][j][2] < 0:
                        im[ii][j][2] = 0
            scipy.misc.imsave(detpath+contents[i][1]+'/light'+contents[i][0], im)
        else:
            a = random.randint(30,100)
            b = random.randint(30,100)
            c = random.randint(30,100)
            for ii in range(im.shape[0]):
                for j in range(im.shape[1]):
                    im[ii][j][0] = im[ii][j][0] + a
                    if im[ii][j][0] > 255:
                        im[ii][j][0] = 255
                    im[ii][j][1] = im[ii][j][1] + b
                    if im[ii][j][1] > 255:
                        im[ii][j][1] = 255
                    im[ii][j][2] = im[ii][j][2] + c
                    if im[ii][j][2] > 255:
                        im[ii][j][2] = 255
            scipy.misc.imsave(detpath+contents[i][1]+'/light'+contents[i][0], im)
        